//
//  YXWithdrawalTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/22.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXWithdrawalTableView.h"
#import "YXCustomSectionView.h"
#import "YXAddPartnerTableView.h"
#import "YXShowPayPasswordView.h"
@interface YXWithdrawalTableView ()<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) UIView *footerView;
@property (nonatomic ,strong) UIButton *withdrawalBtn;
@property (nonatomic ,strong) NSArray *contentArr;
@property (nonatomic ,strong) YXShowPayPasswordView *payPasswordView;

@end
@implementation YXWithdrawalTableView
- (YXShowPayPasswordView *)payPasswordView {
    if (!_payPasswordView) {
        _payPasswordView = [[YXShowPayPasswordView alloc] initWithFrame:self.bounds];
        YXWeakSelf
        [_payPasswordView setPushResultsView:^(NSString *password){
            NSLog(@"%@",password);
            
        }];
    }
    return _payPasswordView;
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"可提余额",@"提现金额",@"手  续  费",@"到账金额"];
    }
    return _titleArr;
}


- (UIButton *)withdrawalBtn {
    if (!_withdrawalBtn) {
        _withdrawalBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _withdrawalBtn.frame = CGRectMake(20, 60, kScreenWidth - 40, 50);
        _withdrawalBtn.backgroundColor = [UIColor py_colorWithHexString:@"#CBCCCD"];
        _withdrawalBtn.layer.masksToBounds = YES;
        _withdrawalBtn.layer.cornerRadius = 8.0f;
        [_withdrawalBtn setTitle:@"提现" forState:(UIControlStateNormal)];
        [_withdrawalBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_withdrawalBtn addTarget:self action:@selector(withdrawalBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _withdrawalBtn;
}

- (void)withdrawalBtnAction:(UIButton *)sender {
    
     UILabel *lab2 = [self viewWithTag:893];
    self.ClickWithdrawalBtnBlock(lab2.text);
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight-self.titleArr.count * 50 -80 - 10 - 150 ))];
        _footerView.backgroundColor = [UIColor whiteColor];
        [_footerView addSubview:self.withdrawalBtn];
    }
    return _footerView;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = color_backColor;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
        self.scrollEnabled = NO;
        self.delegate = self;
        self.dataSource = self;
        self.tableFooterView = self.footerView;
        [self registerClass:[YXWithdrawalTableViewCell class] forCellReuseIdentifier:@"YXWithdrawalTableViewCell"];
        [self registerClass:[YXAddPartnerTableViewcell class] forCellReuseIdentifier:@"YXAddPartnerTableViewcell"];
    }
    return self;
}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        YXAddPartnerTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXAddPartnerTableViewcell" forIndexPath:indexPath];
        cell.titleLab.text = @"提现金额";
        cell.contentTF.placeholder = @"请输入提现金额";
        cell.contentTF.delegate = self;
        cell.contentTF.keyboardType = UIKeyboardTypeNumberPad;
        cell.contentTF.tag = 888;
        [cell.contentTF addTarget:self action:@selector(textFiledDidChange:) forControlEvents:UIControlEventEditingChanged];
        return cell;
    }else {
        YXWithdrawalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXWithdrawalTableViewCell" forIndexPath:indexPath];
        cell.titleLab.text = self.titleArr[indexPath.row];
        NSString *availableAmount = [NSString stringWithFormat:@"%@",[YXUserInfoManager getUserInfo].availableAmount];
        self.contentArr = @[availableAmount,@"",@"系统自动计算",@"系统自动计算"];
        cell.contentLab.text = self.contentArr[indexPath.row];
        cell.contentLab.tag = 890 + indexPath.row;
        if (indexPath.row == 2) {
            UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
            [btn setImage:[UIImage imageNamed:@"问号"] forState:(UIControlStateNormal)];
            [btn addTarget:self action:@selector(poundageAction) forControlEvents:(UIControlEventTouchUpInside)];
            [cell.contentView addSubview:btn];
            __weak typeof(cell) weakSelf = cell;
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(weakSelf.mas_centerY);
                make.right.equalTo(weakSelf.mas_right).offset(-30);
                [btn sizeToFit];
            }];
            
        }
        
        return cell;
    }
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YXCustomSectionView *sectionView = [[YXCustomSectionView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 80)) withTieleArr:@[@"累计分润提现",@"累计达标提现"] withImageArr:nil];
    
    [sectionView setSelectBtnClick:^(NSInteger index) {
        NSLog(@"%ld",index);
    }];
    return sectionView;
}



- (void)poundageAction {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提现手续费" message:@"2元/笔" preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *alert = [UIAlertAction actionWithTitle:@"关闭" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:alert];
    
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}


#pragma mark - TextFieldDelegate
- (void)textFiledDidChange:(UITextField *)textField
{
    CGFloat availableAmount = [[YXUserInfoManager getUserInfo].availableAmount floatValue];
    CGFloat amount = [textField.text floatValue];
    UILabel *lab1 = [self viewWithTag:892];
    UILabel *lab2 = [self viewWithTag:893];
    if (amount > availableAmount || amount <= 2.00) {
        [self.withdrawalBtn setBackgroundColor:[UIColor py_colorWithHexString:@"#CBCCCD"]];
        self.withdrawalBtn.userInteractionEnabled = NO;
        lab1.text = @"系统自动计算";
        lab2.text = @"系统自动计算";
        
    }else{
     
        if (textField.text.length > 0) {
            [self.withdrawalBtn setBackgroundColor:color_globalColor];
            self.withdrawalBtn.userInteractionEnabled = YES;
            lab1.text = @"2.00";
            lab2.text = [NSString stringWithFormat:@"%.2f",amount - 2.00];
        }else if (textField.text.length == 0){
            [self.withdrawalBtn setBackgroundColor:[UIColor py_colorWithHexString:@"#CBCCCD"]];
            self.withdrawalBtn.userInteractionEnabled = NO;
            lab1.text = @"系统自动计算";
            lab2.text = @"系统自动计算";
        }
    }
   

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    CGFloat availableAmount = [[YXUserInfoManager getUserInfo].availableAmount floatValue];
    CGFloat amount = [textField.text floatValue];
    UILabel *lab1 = [self viewWithTag:892];
    UILabel *lab2 = [self viewWithTag:893];
    if (amount > availableAmount || amount <= 2.00) {
        [self.withdrawalBtn setBackgroundColor:[UIColor py_colorWithHexString:@"#CBCCCD"]];
        self.withdrawalBtn.userInteractionEnabled = NO;
        lab1.text = @"系统自动计算";
        lab2.text = @"系统自动计算";
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"余额不足" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *alert = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertController addAction:alert];
        
        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
        
    }else{
        
        if (textField.text.length > 0) {
            [self.withdrawalBtn setBackgroundColor:color_globalColor];
            self.withdrawalBtn.userInteractionEnabled = YES;
            lab1.text = @"2.00";
            lab2.text = [NSString stringWithFormat:@"%.2f",amount - 2.00];
        }else if (textField.text.length == 0){
            [self.withdrawalBtn setBackgroundColor:[UIColor py_colorWithHexString:@"#CBCCCD"]];
            self.withdrawalBtn.userInteractionEnabled = NO;
            lab1.text = @"系统自动计算";
            lab2.text = @"系统自动计算";
        }
    }
    
    [textField resignFirstResponder];
    [textField endEditing:YES];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITextField *tf = [self viewWithTag:888];
    [tf resignFirstResponder];
}

@end


@implementation YXWithdrawalTableViewCell

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel setLabelWithText:@"" fontSize:16 color:color_textOne];
    }
    return _titleLab;
}

- (UILabel *)contentLab {
    if (!_contentLab) {
        _contentLab = [UILabel setLabelWithText:@"" fontSize:16 color:color_textThree];
        _contentLab.textColor = color_globalColor;
    }
    return _contentLab;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor= color_lineColor;
    }
    return _lineView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.contentLab];
        [self.contentView addSubview:self.lineView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(10);
        make.centerY.equalTo(weakSelf.mas_centerY);
        [_titleLab sizeToFit];
    }];
    
    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(45);
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150, 50));
    }];
    
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(0);
        make.right.equalTo(weakSelf.mas_right).offset(0);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 1));
    }];
}
@end









